package cn.oop.plane;

import java.awt.*;
import java.awt.event.KeyEvent;

public class MyPlane {
    Image img;
    double x, y;
    double speed = 10;
    private boolean left, up, right, down;

    private boolean live = true;
    
    public Rectangle getRect() {
        Rectangle r = new Rectangle((int) x, (int) y, img.getWidth(null),
                img.getHeight(null));
        return r;
    }

    public void draw(Graphics g) {
        if (live) {
            g.drawImage(img, (int) x, (int) y, null);
            Move();
        } else {
            g.drawImage(img, (int) x, (int) y, null);
        }
    }

    public void Move() {
        if (left) {
            x -= speed;
        }
        if (right) {
            x += speed;
        }
        if (up) {
            y -= speed;
        }
        if (down) {
            y += speed;
        }

    }

    public void reduceDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 37:
                left = true;
                break;
            case 38:
                up = true;
                break;
            case 39:
                right = true;
                break;
            case 40:
                down = true;
                break;
            default:
                break;
        }
    }

    public void addDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 37:
                left = false;
                break;
            case 38:
                up = false;
                break;
            case 39:
                right = false;
                break;
            case 40:
                down = false;
                break;
            default:
                break;
        }
    }

    public MyPlane(String imgpath, double x, double y) {
        super();

        this.img = ImageUtil.scaleImage(30, 30, imgpath);
        this.x = x;
        this.y = y;
    }

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public void peng(String imgpath) {
        this.img = ImageUtil.scaleImage(30, 30, imgpath);
    }

}
